<?php

/*
 * part of SA-Connector
 *
 */

/**
 * Autoloader of classes
 *
 * @author Druid
 */
class SA_Loader {

    /**
     *
     * @var type array of strings
     */
    protected static $_dirs = '';

    /**
     *
     * @var type string
     */
    protected static $_fileExtensions = '.php';

    /**
     *
     * @param type $dir string
     */
    public static function addDir($dir='') {
        if(!is_array(self::$_dirs)) {
            self::$_dirs = array();
            $listDirs = str_getcsv(get_include_path(), PATH_SEPARATOR);
            foreach($listDirs as $curDir) {
                array_push(self::$_dirs, realpath($curDir));
            }
        }
        if(is_string($dir) AND strlen($dir) > 0)
            array_push(self::$_dirs, realpath($dir));
        self::$_dirs = array_unique(self::$_dirs);
    }

    static public function Autoload($class) {
        if(!is_array(self::$_dirs))
            self::addDir();
        foreach(self::$_dirs as $curDir) {
            $classPath = str_replace('_', DIRECTORY_SEPARATOR, $class);
            $fileName = $curDir . DIRECTORY_SEPARATOR . $classPath . self::$_fileExtensions;
            if(file_exists($fileName)) {
                require_once ($fileName);
                if(class_exists($class))
                    break;
            }
        }
        if(!class_exists($class)) {
            throw new Exception('class ' . $class . ' not found!');
        }
    }

}